package com.linxi.handy.component.module;

import com.linxi.handy.common.Constants;

import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import java.awt.*;

/**
 * 滚动窗格展示
 *
 * @author user
 */
public class ExecSqlResultDisplay extends JScrollPane {

    public JTextPane textPane = new JTextPane();
    SimpleAttributeSet attrSet = new SimpleAttributeSet();

    public ExecSqlResultDisplay() {
        textPane.setEditable(false);
        textPane.setFont(Constants.DEFAULT_FONT_13);
        this.setViewportView(textPane);
    }

    public void setText(String sql, String error) {
        if (sql == null) {
            return;
        }

        //获得文本对象
        Document docs = textPane.getDocument();
        try {
            //对文本进行追加
            docs.insertString(docs.getLength(), sql + "\r\n", null);
            if (error == null) {
                docs.insertString(docs.getLength(), "执行成功！\r\n", null);
            } else {
                StyleConstants.setForeground(attrSet, Color.red);
                docs.insertString(docs.getLength(), "错误：" + error + "\r\n", attrSet);
            }
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    }

    public void setText(String sql, int i, String error) {
        if (sql == null) {
            return;
        }

        //获得文本对象
        Document docs = textPane.getDocument();
        try {
            //对文本进行追加
            docs.insertString(docs.getLength(), sql + "\r\n", null);
            docs.insertString(docs.getLength(), "受影响行数：" + i + "\r\n", null);
            if (error == null) {
                docs.insertString(docs.getLength(), "执行成功！\r\n", null);
            } else {
                StyleConstants.setForeground(attrSet, Color.red);
                docs.insertString(docs.getLength(), "错误：" + error + "\r\n", attrSet);
            }
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    }

    public void setText(String[] sql, int error) {
        if (sql == null) {
            return;
        }

        //获得文本对象
        Document docs = textPane.getDocument();
        try {
            if (error == -1) {
                StyleConstants.setForeground(attrSet, Color.PINK);
                //对文本进行追加
                docs.insertString(docs.getLength(), "事务失败，已回滚，回滚成功\r\n", attrSet);
                return;
            }
            if (error == -2) {
                StyleConstants.setForeground(attrSet, Color.red);
                //对文本进行追加
                docs.insertString(docs.getLength(), "事务失败，已回滚，回滚失败\r\n", attrSet);
                return;
            }
            for (int i = 0; i < sql.length; i++) {
                //对文本进行追加
                docs.insertString(docs.getLength(), sql[i] + "\r\n", null);
            }
            //对文本进行追加
            docs.insertString(docs.getLength(), "全部执行成功！\r\n", null);
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    }
}
